package independentsetproblem;

import java.io.IOException;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import org.jgrapht.UndirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.ext.DOTExporter;
import org.jgrapht.ext.VertexNameProvider;

public class DotExport
{
    UndirectedGraph<Vertex, DefaultEdge> ExpGraph;
    String path;

    public DotExport (UndirectedGraph<Vertex, DefaultEdge> g, String p)
    {
       ExpGraph = g;
       path = p;
    }

    public void export()
    {
        DOTExporter<Vertex, DefaultEdge> exportToDot =
           new DOTExporter<Vertex, DefaultEdge>(new NameProvider(),new LabelProvider(), null);

        try
        {
           System.out.println("\n"+"Writing result to " + path);
           FileOutputStream outputStream = new FileOutputStream(path);
           OutputStreamWriter writer =
                   new OutputStreamWriter(outputStream, "Cp1251");
           BufferedWriter bufWriter = new BufferedWriter(writer);
           exportToDot.export(bufWriter, ExpGraph);
           bufWriter.close();
         }
        catch (IOException e)
        {
            System.out.println("Can not write into file");
            e.printStackTrace();
        }
    }

    private class NameProvider implements VertexNameProvider<Vertex>
    {
        @Override
        public String getVertexName(Vertex v)
        {
            return v.toString();
        }
    }

    private class LabelProvider implements VertexNameProvider<Vertex>
    {
        @Override
        public String getVertexName(Vertex v)
        {
            if(v.getInMaxIndSet())
            {
                return "Max Independent Set Member";
            }
            return v.toString();
        }
    }
}




